package cn.com.chengmandian.bike.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;

import java.util.List;

public class DingRobotUtil {
    public static void main(String[] args) throws Exception {
    }

    public static void sendTextMessage(String message, String robot) throws ApiException {
        sendTextMessage(message, null, false, robot);
    }

    public static void sendLinkMessage(String messageUrl,String title,String text, String robot) throws ApiException {
        sendLinkMessage(messageUrl, title, text,null,false,robot);
    }

    public static void sendTextMessage(String message, List<String> atUsers, boolean isAtAll, String robot) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=" + robot);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(message);
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        if (atUsers != null && atUsers.size() > 0) {
            at.setAtUserIds(atUsers);
        }
        at.setIsAtAll(isAtAll);
        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
        System.out.println(response.getBody());
    }

    public static void sendLinkMessage(String messageUrl,String title,String text, List<String> atUsers, boolean isAtAll, String robot) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=" + robot);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("link");
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl(messageUrl);
        link.setPicUrl("");
        link.setTitle(title);
        link.setText(text);
        request.setLink(link);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        if (atUsers != null && atUsers.size() > 0) {
            at.setAtUserIds(atUsers);
        }
        at.setIsAtAll(isAtAll);
        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
        System.out.println(response.getBody());
    }
}
